﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;

namespace Microsoft.Services.Clab.UI.Mvvmc.VisualStudio.TemplateWizards
{
    public partial class MvvmcItemTemplateWindow : Window, INotifyPropertyChanged
    {
        public MvvmcItemTemplateWindow()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        #region Properties

        #region Prefix Property
        public string Prefix
        {
            get { return this.prefix; }
            set { if (this.prefix != value) { this.prefix = value; this.OnPropertyChanged("Prefix"); } }
        }
        private string prefix;
        #endregion

        #region ControllerName Property
        public string ControllerName
        {
            get { return this.controllerName; }
            set { if (this.controllerName != value) { this.controllerName = value; this.OnPropertyChanged("ControllerName"); } }
        }
        private string controllerName;
        #endregion

        #region ControllerLogicalName Property
        public string ControllerLogicalName
        {
            get { return this.controllerLogicalName; }
            set { if (this.controllerLogicalName != value) { this.controllerLogicalName = value; this.OnPropertyChanged("ControllerLogicalName"); } }
        }
        private string controllerLogicalName;
        #endregion

        #region ControllerBaseTypeAssemblyQualifiedName Property
        public string ControllerBaseTypeAssemblyQualifiedName
        {
            get { return this.controllerBaseTypeAssemblyQualifiedName; }
            set { if (this.controllerBaseTypeAssemblyQualifiedName != value) { this.controllerBaseTypeAssemblyQualifiedName = value; this.OnPropertyChanged("ControllerBaseTypeAssemblyQualifiedName"); } }
        }
        private string controllerBaseTypeAssemblyQualifiedName;
        #endregion

        #region ViewModelName Property
        public string ViewModelName
        {
            get { return this.viewModelName; }
            set { if (this.viewModelName != value) { this.viewModelName = value; this.OnPropertyChanged("ViewModelName"); } }
        }
        private string viewModelName;
        #endregion

        #region ViewModelBaseTypeAssemblyQualifiedName Property
        public string ViewModelBaseTypeAssemblyQualifiedName
        {
            get { return this.viewModelBaseTypeAssemblyQualifiedName; }
            set { if (this.viewModelBaseTypeAssemblyQualifiedName != value) { this.viewModelBaseTypeAssemblyQualifiedName = value; this.OnPropertyChanged("ViewModelBaseTypeAssemblyQualifiedName"); } }
        }
        private string viewModelBaseTypeAssemblyQualifiedName;
        #endregion

        #region ViewName Property
        public string ViewName
        {
            get { return this.viewName; }
            set { if (this.viewName != value) { this.viewName = value; this.OnPropertyChanged("ViewName"); } }
        }
        private string viewName;
        #endregion

        #region ViewBaseTypeAssemblyQualifiedName Property
        public string ViewBaseTypeAssemblyQualifiedName
        {
            get { return this.viewBaseTypeAssemblyQualifiedName; }
            set { if (this.viewBaseTypeAssemblyQualifiedName != value) { this.viewBaseTypeAssemblyQualifiedName = value; this.OnPropertyChanged("ViewBaseTypeAssemblyQualifiedName"); } }
        }
        private string viewBaseTypeAssemblyQualifiedName;
        #endregion

        #region BaseClassesSets Property
        public Dictionary<string, string[]> BaseClassesSets
        {
            get { return this.baseClassesSets; }
            set { if (this.baseClassesSets != value) { this.baseClassesSets = value; this.OnPropertyChanged("BaseClassesSets"); } }
        }
        private Dictionary<string, string[]> baseClassesSets;
        #endregion

        #region SelectedBaseClassesSet Property
        public string SelectedBaseClassesSet
        {
            get { return this.selectedBaseClassesSet; }
            set
            {
                if (this.selectedBaseClassesSet != value)
                {
                    this.selectedBaseClassesSet = value;
                    this.OnPropertyChanged("SelectedBaseClassesSet");
                    this.FillPropertiesWithSet(value);
                }
            }
        }
        private string selectedBaseClassesSet;
        #endregion

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string name)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(name));
            }

            if (name == "Prefix")
            {
                this.ControllerName = this.Prefix + "Controller";
                this.ViewModelName = this.Prefix + "ViewModel";
                this.ViewName = this.Prefix + "MainView";
            }
        }

        #endregion

        #region Commands

        private void Ok_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void Annulla_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void FillPropertiesWithSet(string setName)
        {
            if (this.BaseClassesSets.ContainsKey(setName))
            {
                var set = this.BaseClassesSets[setName];
                this.ControllerBaseTypeAssemblyQualifiedName = set[0];
                this.ViewModelBaseTypeAssemblyQualifiedName = set[1];
                this.ViewBaseTypeAssemblyQualifiedName = set[2];
            }
            else
            {
                this.ControllerBaseTypeAssemblyQualifiedName = string.Empty;
                this.ViewModelBaseTypeAssemblyQualifiedName = string.Empty;
                this.ViewBaseTypeAssemblyQualifiedName = string.Empty;
            }
        }

        #endregion
    }
}
